﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

using System.Collections.Generic;
using SmartProject.Data;

namespace SmartProject.AppSpace
{
    public partial class Setup : UI.BasePage
    {
        private List<Entity> _entities;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.SetPage("Setup Wizard", "Based on your business rules, you can configure the appropriate preference on how you want to set a user up.");
                if (UI.BasePage.IsFirstTimePreferenceSetup)
                {
                    this.DisplayErrorOrWarning("Please determine account setup preference.");
                }

                RefreshView();

                DataTable preferences = new DataTable();
                preferences.Columns.Add("Name");
                preferences.Columns.Add("Options");
                foreach (Entity entity in _entities)
                {
                    foreach (BusinessRulePreference preference in entity.Preferences)
                    {
                        bool loadPreference = true;
                        if (!UI.BasePage.IsFirstTimePreferenceSetup)
                        {
                            if (preference.IsInitialSetupOnly)
                            {
                                loadPreference = false;
                            }
                        }

                        if (loadPreference)
                        {
                            DataRow row = preferences.NewRow();
                            row["Name"] = preference.Name;
                            preferences.Rows.Add(row);
                        }
                    }
                }

                if (preferences.Rows.Count > 0)
                {
                    this.LstPreferences.DataSource = preferences;
                    this.LstPreferences.DataBind();
                }
                else
                {
                    this.DisplayErrorOrWarning("No preference avaliable to be configured.");
                    this.BtnSave.Enabled = false;
                }
            }
        }

        private void RefreshView()
        {
            _entities = new List<Entity>();
            BusinessObjects.User user = new BusinessObjects.User();
            _entities.Add(user);
        }

        protected void LstPreferences_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            Label lbl = (Label)e.Item.FindControl("LblName");
            RadioButtonList rbn = (RadioButtonList)e.Item.FindControl("RbnOptions");
            foreach (Entity entity in _entities)
            {
                foreach (BusinessRulePreference preference in entity.Preferences)
                {
                    if (preference.Name == lbl.Text)
                    {
                        BusinessRule selected = preference.GetSelectedBusinessRule();
                        int selectedIndex = -1;
                        List<string> options = new List<string>();
                        for (int i = 0; i < preference.BusinessRules.Length; i++)
                        {
                            BusinessRule rule = preference.BusinessRules[i];
                            string option = String.Format("<b>{0}</b><br/>{1}", rule.Name, rule.Description);
                            options.Add(option.Replace("\r\n", "<br/>"));
                            if (selected == rule)
                            {
                                selectedIndex = i;
                            }
                        }
                        rbn.DataSource = options;
                        rbn.DataBind();

                        if (selectedIndex > -1)
                        {
                            rbn.SelectedIndex = selectedIndex;
                        }
                        break;
                    }
                }
            }
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            RefreshView();
            foreach (ListViewDataItem item in this.LstPreferences.Items)
            {
                RadioButtonList rbn = (RadioButtonList)item.FindControl("RbnOptions");
                Label lbl = (Label)item.FindControl("LblName");

                foreach (Entity entity in _entities)
                {
                    foreach (BusinessRulePreference preference in entity.Preferences)
                    {
                        if (preference.Name == lbl.Text)
                        {
                            BusinessRule selected = preference.BusinessRules[rbn.SelectedIndex];
                            preference.Save(selected.Name);
                        }
                    }
                }
            }
            Response.Redirect("~/AppSpace/MyPage.aspx", true);
        }

        public override void RefreshPage(object obj)
        {
            // Do nothing.
        }

        public override AjaxControlToolkit.ModalPopupExtender Popup(string name)
        {
            return null;
        }
    }
}
